package pers.lance.fsm.ssh;

import pers.lance.fsm.util.FileUtil;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * server
 */
public class Server {

    ServerSocket serverSocket = null;
    Socket socket = null;
    int port;

    public Server(int port) {
        this.port = port;
    }

    /**
     * execute serversocket
     */
    public void start() {
        try {
            serverSocket = new ServerSocket(9999);
            System.out.println("server running....");
            while (true) {
                socket = serverSocket.accept();
                System.out.println("client coming.....");//dsadsdadsaaa
                //start input output data
                new ServerInvokeThread(socket).start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    class ServerInvokeThread extends Thread {
        Socket socket = null;

        ServerInvokeThread(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                System.out.println("server input running");
                OutputStream out = socket.getOutputStream();
                InputStream inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String temp = null;
                while ((temp = bufferedReader.readLine()) != null) {
                    System.out.println("accept client msg: " + temp);
                    //invoke fileutil
                    String[] strings = temp.split(" ");
                    if (strings[0].toLowerCase().equals("ls")) {

                    } else if (strings[0].toLowerCase().equals("cp")) {
                        File from = new File(strings[1]);
                        File to = new File(strings[2]);
                        if (from.isDirectory()) {
                            FileUtil.copyDirectory(from, to);
                            System.out.println("copy success.....");
                        } else {
                            FileUtil.copyFile(from, to);
                            System.out.println("copy success");
                        }

                    } else if (strings[0].toLowerCase().equals("rm")) {
                        File file = new File(strings[0]);
                        FileUtil.removeFile(file);
                        System.out.println("remove success");
                    } else if (strings[0].toLowerCase().equals("ls")) {
                        File file = new File(strings[1]);
                        FileUtil.listFiles(file);
                    } else {
                        System.out.println("no command");
                    }
                    out.write("operate success\n".getBytes());

                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }

        }
    }
}
